package SQL

import (
	"chat/service"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type SQLConfig struct {
	Address  string
	UserName string
	Password string
	Database string
	Port     string
}

func (s SQLConfig) New() string {
	return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", s.UserName, s.Password, s.Address, s.Port, s.Database)
}
func ConnectSQL() (*gorm.DB, error) {
	sqlConfig := SQLConfig{
		Address:  "101.42.51.193",
		UserName: "qigeyu",
		Password: "Wsyya0928!@#",
		Database: "chat_room",
		Port:     "3306",
	}
	addr := sqlConfig.New()
	db, err := gorm.Open(mysql.Open(addr), &gorm.Config{})
	if err != nil {
		return nil, err
	}
	//_ = db.Migrator().DropTable(&service.UserService{})
	err = db.AutoMigrate(&service.UserService{}, &service.WsStruct{}, &service.UserMusic{})
	if err != nil {
		fmt.Println(err)
		return nil, err
	}
	fmt.Println(db)
	return db, nil
}
